/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.flow.order;

import com.lxk.event.oms.enums.OrderEnums;
import com.lxk.oms.cart.domain.vo.OmsCartVo;
import com.lxk.oms.order.domain.bo.OmsOrderBo;
import com.lxk.oms.order.domain.bo.OmsOrderItemBo;
import com.lxk.oms.order.domain.req.CreateOrderReq;
import com.lxk.oms.order.domain.vo.OmsOrderItemVo;
import com.lxk.oms.order.domain.vo.OmsOrderVo;
import com.lxk.service.pms.response.PmsProductSkuDto;
import com.lxk.service.pms.response.PmsProductSpuDto;
import com.lxk.service.ums.response.UmsUserDto;
import io.github.linpeilie.annotations.AutoMapper;
import io.github.linpeilie.annotations.AutoMapping;
import io.github.linpeilie.annotations.AutoMappings;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.dromara.common.core.domain.FlowBaseContext;

import java.math.BigDecimal;
import java.util.List;

/**
 * 下单上下文
 *
 * @author heyi
 * <p>
 * 2024/8/20
 */
@EqualsAndHashCode(callSuper = true)
@Data
@AutoMapper(target = OmsOrderBo.class, reverseConvertGenerate = false)
public class OrderContext extends FlowBaseContext {

    /**
     * 商家ID
     */
    private Long merId;


    /**
     * 提货方式
     */
    @AutoMappings(value = {
        @AutoMapping(target = "deliveryType", expression = "java(source.getDeliveryType().getCode())")
    })
    private OrderEnums.DeliveryType deliveryType = OrderEnums.DeliveryType.KD;

    /**
     * 用户备注
     */
    private String userRemark;

    /**
     * 用户IP
     */
    private String userIp;

    /**
     * 订单类型
     */
    @AutoMapping(target = "orderType", expression = "java(source.getOrderType().getCode())")
    private OrderEnums.OrderType orderType;


    /**
     * 需要库存的订单
     */
    @AutoMappings(value = {
        @AutoMapping(target = "needStock", expression = "java(source.getNeedStockCode())")
    })
    private Boolean needStock = true;


    public Integer getNeedStockCode() {
        return needStock ? OrderEnums.NeedStock.YES.getCode() : OrderEnums.NeedStock.NOT.getCode();
    }

    /**
     * 虚拟SKU
     */
    private List<VirtualSkuInfo> virtualSkuInfoList;


//    /**
//     * 需要库存的订单
//     *
//     * @return
//     */
//    public Boolean GetNeedStock() {
//        return OrderEnums.OrderType.NEED_STOCK.contains(orderType);
//    }

    /**
     * 用户信息
     */
    @AutoMappings(value = {
        @AutoMapping(source = "umsUserDto", target = "userId", expression = "java(source.getUmsUserDto().getUserId())"),
    })
    private UmsUserDto umsUserDto;

    /**
     * 联系人名称
     */
    private String receiverName;

    /**
     * 联系人手机
     */
    private String receiverMobile;
    /**
     * 联系人详细地址
     */
    private String receiverDetailAddress;

    /**
     * 订单金额
     */
    private BigDecimal totalPrice = BigDecimal.ZERO;

    /**
     * 订单成本总价
     */
    private BigDecimal orderCostPrice = BigDecimal.ZERO;

    /**
     * 订单市场总价
     */
    private BigDecimal orderMarkerPrice = BigDecimal.ZERO;

    /**
     * 订单利润=totalPrice-orderCostPrice
     */
    private BigDecimal orderProfitPrice = BigDecimal.ZERO;

    /**
     * 订单优惠金额
     */
    private BigDecimal discountPrice = BigDecimal.ZERO;

    /**
     * 应付金额=订单金额-订单优惠金额
     */
    private BigDecimal payPrice = BigDecimal.ZERO;

    /**
     * 实际支付金额，一般等于应付金额， 用于拍下商家改价场景
     */
    private BigDecimal actPayPrice = BigDecimal.ZERO;

    /**
     * 实收款
     */
    private BigDecimal actReceiveAmount = BigDecimal.ZERO;

    /**
     * 支付金额
     */
    private BigDecimal payPoint = BigDecimal.ZERO;

    /**
     * 订单省份ID
     */
    private Long orderProvinceId;

    /**
     * 订单省份名称
     */
    private String orderProvinceName;

    /**
     * 订单城市ID
     */
    private Long orderCityId;

    /**
     * 订单城市名称
     */
    private String orderCityName;

    /**
     * 订单区县ID
     */
    private Long orderDistrictId;

    /**
     * 订单区县名称
     */
    private String orderDistrictName;

    /**
     * 是否保存到入库
     */
    private Boolean saveDb = true;


    /**
     * 商品信息
     */
    private List<SkuInfo> skuInfoList;


    /**
     * 订单详细表
     */
    private List<OmsOrderItemBo> orderItemBoList;

    /**
     * 订单名称
     */
    private String orderName;


    /**
     * 订单处理结果
     */
    private OrderResult orderResult;


    /**
     * 购物车订单
     */
    private List<OmsCartVo> cartList;


    /**
     * 下单类型
     */
    private CreateOrderReq.CreateType createType;


    public void addOrderItem(OmsOrderItemBo add) {
        orderItemBoList.add(add);
    }


    public Long getUserId() {
        return umsUserDto.getUserId();
    }

    public void addActPayPrice(BigDecimal price) {
        this.actPayPrice = actPayPrice.add(price);
        this.actReceiveAmount = actReceiveAmount.add(price);
    }

    public void addTotalPrice(BigDecimal price) {
        this.totalPrice = totalPrice.add(price);
    }

    public void addPayPrice(BigDecimal price) {
        this.payPrice = payPrice.add(price);
    }

    public void addOrderCostPrice(BigDecimal price) {
        this.orderCostPrice = orderCostPrice.add(price);
    }

    public void addOrderMarkerPrice(BigDecimal price) {
        this.orderMarkerPrice = orderMarkerPrice.add(price);
    }

    public void addOrderProfitPrice(BigDecimal price) {
        this.orderProfitPrice = orderProfitPrice.add(price);
    }

    public void addPayPoint(BigDecimal point) {
        this.payPoint = payPoint.add(point);
    }

    @Data
    @AutoMapper(target = CreateOrderReq.CreateVirtualOrderItem.class)
    public static class VirtualSkuInfo {

        /**
         * 商家信息
         */
        private Long merId;

        /**
         * 购买数量
         */
        private Integer count;

        /**
         * 金额
         */
        private BigDecimal amount;

        /**
         * Sku名称
         */
        private String skuName;


        /**
         * 虚拟spuId 可能是优惠券，券包，活动报名
         */
        private Long virtualSpuId;

        /**
         * 虚拟skuId 可能是优惠券，券包，活动报名
         */
        private Long virtualSkuId;
    }

    @Data
    public static class SkuInfo {

        /**
         * 商品信息
         */
        private PmsProductSpuDto spu;
        /**
         * 商家信息
         */
        private Long merId;
        /**
         * SKU信息
         */
        private List<PmsProductSkuDto> pmsProductSkuDtoList;
        /**
         * 购买数量
         */
        private Integer count;
    }

    /**
     * 订单返回值
     */
    @Data
    public static class OrderResult {
        /**
         * 订单信息
         */
        private OmsOrderVo order;

        /**
         * 订单详细表
         */
        private List<OmsOrderItemVo> orderItemVoList;
    }

}
